using System;
using System.Collections.Generic;
using System.ServiceModel;
using System.ServiceModel.Channels;
using Framework.Remoting.Conventions;

namespace Framework.Remoting.Client
{
    public class CachedChannelFactoryFactory : IChannelFactoryFactory
    {
        private readonly IDictionary<Type, object> cache = new Dictionary<Type, object>();

        public IChannelFactory<T> Create<T>(WcfServiceInfo wcfServiceInfo)
        {
            var channelFactoryType = typeof(IChannelFactory<T>);

            if (!cache.ContainsKey(channelFactoryType))
            {
                cache[channelFactoryType] = new ChannelFactory<T>(wcfServiceInfo.Binding, new EndpointAddress(wcfServiceInfo.Uri));
            }

            return (IChannelFactory<T>)cache[channelFactoryType];
        }
    }
}